<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/7
 * Time: 10:35
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Systemnotice extends Base
{
    /**
     * 系统消息列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $notice_title = trim(input('notice_title'), ' ');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where = [];
            if(isset($notice_title) && $notice_title != ''){
                $where['notice_title'] = ['like', '%' . $notice_title . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['notice_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->field('notice_id, notice_title, notice_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('notice_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发布系统消息
     * @return mixed|\think\response\Json
     */
    public function noticeAdd(){
        if(Request::instance()->isPost()){
            $notice_title = trim(input('notice_title'), ' ');
            $notice_content = trim(input('notice_content'), ' ');
            $notice_user = input('notice_user');
            $notice_range = input('notice_range');
            $insert = [
                'notice_title' => $notice_title,
                'notice_content' => $notice_content,
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_type' => 2,
                'notice_time' => date('Y-m-d H:i:s'),
                'notice_range' => $notice_range,
                'notice_committee_id' => 0
            ];
            if($notice_range == 1){
                $insert['notice_user'] = -1;
            }else{
                $insert['notice_user'] = ',0,' . $notice_user . ',';
            }
            $res = Db::table('up_notice')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        $user = Db::table('up_u_user')
            ->where('user_status', 1)
            ->field('user_id, user_username, user_phone')
            ->select();
        return $this->fetch('systemnotice/noticeadd', ['user' => $user]);
    }
    
    /**
     * 消息详情
     * @return mixed
     */
    public function noticeDetail(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_id, notice_title, notice_content, notice_time, notice_range, notice_user, 
            notice_type, notice_committee_id')
            ->find();
        if($notice['notice_range'] == 2){
            $user_ids = trim($notice['notice_user'], ',');
            $user_name = Db::table('up_u_user')
                ->where('user_id', 'IN', $user_ids)
                ->column('user_username');
            $notice['notice_user'] = implode(',', $user_name);
        }
        return $this->fetch('systemnotice/noticedetail', ['notice' => $notice]);
    }
}